package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TCTxnStatisMapper;
import cc.rengu.igas.momp.common.entity.TCTxnStatis;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 批量模块商户交易统计表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @since 2020-06-30 02:35
 */

public class TCTxnStatisMapperImpl implements TCTxnStatisMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    /**
     * 使用主键查找单条商户交易统计表数据数据库操作接口实现
     *
     * @param instId   法人机构号
     * @param statDate 统计日期
     * @param mchntNo  内部商户号/门店号
     * @return cc.rengu.igas.momp.common.entity.TCTxnStatis 批量模块商户交易统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020-06-30 02:34
     */
    @Override
    public TCTxnStatis selectMchntLastMonthTxnStatisticsByPrimaryKey(String instId, String statDate, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntLastMonthTxnStatisticsByPrimaryKey");

        TCTxnStatis tcTxnStatis = new TCTxnStatis();
        tcTxnStatis.setInstId(instId);
        tcTxnStatis.setStatisDate(statDate);
        tcTxnStatis.setMchntNo(mchntNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tcTxnStatis, tcTxnStatis);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("获取法人机构号<{}>的商户编号<{}>的月份<{}>的商户上月日均交易额数据失败!<{}>", instId, mchntNo, statDate, iReturnCode);
            return null;
        }

        return tcTxnStatis;
    }
}
